package com.cslg.ac.demo.service.pet;
import com.cslg.ac.demo.dao.host.HostFosterDAO;
import com.cslg.ac.demo.dao.host.po.HostFosterPO;
import com.cslg.ac.demo.dao.pet.*;
import com.cslg.ac.demo.dao.pet.po.PetAdoptPO;
import com.cslg.ac.demo.dao.pet.po.PetCommonPO;
import com.cslg.ac.demo.dao.pet.po.PetFosterPO;
import com.cslg.ac.demo.dao.user.person.PersonInfoDAO;
import com.cslg.ac.demo.dao.user.team.TeamInfoDAO;
import com.cslg.ac.demo.entity.pet.PetAdopt;
import com.cslg.ac.demo.entity.pet.PetCommon;
import com.cslg.ac.demo.entity.pet.PetFoster;
import com.cslg.ac.demo.entity.user.team.TeamInfo;
import com.cslg.ac.demo.exception.ParamIllegalException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@PropertySource(value = {"classpath:pet.properties"},encoding="utf-8")
public class PetService {
    @Autowired
    PetCommonDAO petCommonDAO;
    @Autowired
    PetFosterDAO petFosterDAO;
    @Autowired
    PetAdoptDAO petAdoptDAO;
    @Autowired
    UserPetDAO userPetDAO;
    @Autowired
    HostFosterDAO hostFosterDAO;
    @Autowired
    TeamInfoDAO teamInfoDAO;
    @Autowired
    PersonInfoDAO personInfoDAO;
    @Autowired
    LongAndLatDAO longAndLatDAO;


    public List<Integer> sortByAddress(Double longitude, double latitude, int petType, int offset, int num){
        List<Integer> userId = longAndLatDAO.userSortByLatAndLong(longitude, latitude, offset, num);
        List<Integer> pet = new ArrayList<>();
        for (Integer id:
                userId) {
            List<Integer> getPet = userPetDAO.selectByUserIdAndType(id, petType);
            if(getPet != null && getPet.size() > 0){
                pet.addAll(getPet);
            }
        }
        if(offset + num > pet.size()){
            return pet.subList(offset, pet.size());
        }
        return pet.subList(offset,offset+num);
        }



    public List<Integer> sortByTime(int petType, int offset, int num){
        switch (petType){
            case(0):
                return petFosterDAO.sortByTime(offset, num);
            case(1):
                return petAdoptDAO.sortByTime(offset, num);
            default:
                return null;
        }

    }


    public Integer[] getPetsByMaster(int masterId,int petType,int offset,int num, int chooseType){
        Integer[] allPet = userPetDAO.selectByUserId(masterId,petType,offset,num);
        if(chooseType == 0){
            return allPet;
        }else if (chooseType == 1) {
            return hostFosterDAO.selectFosterNotSend(masterId, offset, num);
        }else if(chooseType == 2){
            return hostFosterDAO.selectFosterNotBack(masterId, offset, num);
        }else if(chooseType == 3){
            return hostFosterDAO.selectFosterNotFoster(masterId, offset, num);
        }
        return null;

    }

    public PetFoster selectPetFoster(int id){
        PetFosterPO petFosterPO = petFosterDAO.selectById(id);
        if(petFosterPO == null){
            return null;
        }
        PetFoster petFoster = new PetFoster();
        BeanUtils.copyProperties(petFosterPO,petFoster);
        return petFoster;
    }

    public PetAdopt selectPetAdopt(int id){
        PetAdoptPO petAdoptPO = petAdoptDAO.selectById(id);
        if(petAdoptPO == null){
            return null;
        }
        PetAdopt petAdopt = new PetAdopt();
        BeanUtils.copyProperties(petAdoptPO,petAdopt);
        return petAdopt;
    }

    public PetCommon selectPetCommon(int id){
        PetCommonPO petCommonPO = petCommonDAO.selectById(id);
        if(petCommonPO == null){
            return null;
        }
        PetCommon petCommon = new PetCommon();
        BeanUtils.copyProperties(petCommonPO,petCommon);
        return petCommon;
    }

    public void addCommon(PetCommon petCommon){
        PetCommonPO petCommonPO = new PetCommonPO();
        BeanUtils.copyProperties(petCommon,petCommonPO);
        petCommonDAO.insert(petCommonPO);
        userPetDAO.insert(petCommon.getMasterId(),petCommon.getKind());

    }

    public int addFoster(PetFoster petFoster, PetCommon petCommon){
        PetFosterPO petFosterPO = new PetFosterPO();
        BeanUtils.copyProperties(petFoster,petFosterPO);
        addCommon(petCommon);
        int lastInsertId = petCommonDAO.lastInsertId();
        petFosterDAO.insert(lastInsertId);
        return lastInsertId;
    }

    public int addAdopt(PetCommon petCommon, PetAdopt petAdopt){
        addCommon(petCommon);
        int lastInsertId = petCommonDAO.lastInsertId();
        PetAdoptPO petAdoptPO = new PetAdoptPO();
        BeanUtils.copyProperties(petAdopt,petAdoptPO);
        petAdoptPO.setId(lastInsertId);
        petAdoptDAO.insert(petAdoptPO);
        return lastInsertId;
    }

    public void delete(int id){
        petCommonDAO.delete(id);
        petAdoptDAO.delete(id);
        petFosterDAO.delete(id);
    }



}
